<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Kickback_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回回扣的数量
	 */
	public function count_kickbacks($userid){
		$this->db->where('userid', $userid);
		$query = $this->db->get('t_kickback');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到回扣信息
	 */
	public function find_kickbacks($userid,$per_page,$offset){
		$this->db->select('t_kickback.id,t_kickback.userid,t_product.name,t_product.price,t_kickback.rate,t_kickback.newprice');
		$this->db->from('t_kickback');
		$this->db->join('t_user', 't_kickback.userid = t_user.id','left');
		$this->db->join('t_product', 't_kickback.productid = t_product.id','left');
		$this->db->where('userid', $userid);
		$this->db->limit($per_page, $offset);
		$this->db->order_by("id", "desc");
		$query=$this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据ID得到回扣信息
	 */
	public function load($id){
		$this->db->select('t_kickback.id,t_kickback.userid,t_kickback.productid,t_product.name,t_product.price,t_kickback.rate,t_kickback.newprice');
		$this->db->from('t_kickback');
		$this->db->join('t_product', 't_kickback.productid = t_product.id','left');
		$this->db->where('t_kickback.id', $id);
		$query = $this->db->get();
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加回扣信息
	 */
	public function create($kickback){
		$this->db->insert('t_kickback', $kickback);
		return $this->db->insert_id();
	}
	/*
	 * 更新回扣信息
	 */
	public function update($id,$kickback){
		$this->db->where('id', $id);
		$this->db->update('t_kickback', $kickback);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除回扣信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_kickback');
		return $this->db->affected_rows()>0;
	}
}